package cn.gson.crm.model.service;

import cn.gson.crm.model.mapper.ExchangegoodsMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.ExchangeGoods;
import cn.gson.crm.model.pojos.ReplacementDetails;
import cn.gson.crm.model.pojos.Role;
import cn.gson.crm.model.pojos.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class ExchangegoodsService {
    @Autowired
    ExchangegoodsMapper exchangegoodsMapper;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询当前登录人换货记录
    public Map<String,Object> selectexchagood(HttpSession session, Integer eid, Integer page, Integer size){

        eid=fouyuan(session);
        List<ExchangeGoods> exchangeGoods2=exchangegoodsMapper.selectexchagood(eid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<ExchangeGoods> exchangeGoods=exchangegoodsMapper.selectexchagood(eid);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",exchangeGoods);
        map.put("tong",exchangeGoods2);
        return map;
    };
    //根据时间查询换货记录
    public Map<String,Object> selectetime(HttpSession session,Integer eid,String statr,String endtime,Integer page, Integer size){
        eid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<ExchangeGoods> exchangeGoods=exchangegoodsMapper.selectetime(eid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",exchangeGoods);
        return map;
    };
    //换货多条件查询
    public Map<String,Object> selectwhere(HttpSession session,Integer eid,String where,Integer page, Integer size){
        eid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<ExchangeGoods> exchangeGoods=exchangegoodsMapper.selectwhere(eid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",exchangeGoods);
        return map;
    };
    //查详情
    public List<ReplacementDetails> selectdat(Integer eid){
        return exchangegoodsMapper.selectdat(eid);
    };
}
